/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Project;

import CommonData.CommonDataManager;
import DataModel.*;
import Project.DataLayer.ProjectDataManager;
import Organization.OrganizationManager;
import java.util.HashMap;
import java.util.Date;
import Utilities.Constants;

/**
 *
 * @author Carlos
 */
public class ProjectManager {


    // <editor-fold defaultstate="collapsed" desc="Attributes">

    // Indexamos las solicitudes incompletas por usuario
    private HashMap<Integer, Project[]> _userRequests;

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Singleton">
    private static ProjectManager _instance = new ProjectManager();

    private ProjectManager()
    {
        _userRequests = new HashMap<Integer, Project[]>();
    }

    public static ProjectManager GetInstance()
    {
        return _instance;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Public Methods">
    public Project GetProject(int projectId)
    {
        return ProjectDataManager.GetInstance().GetProject(projectId);
    }

    public void AssingProjectOfficer(int projectId, int officerId)
    {
        ProjectDataManager.GetInstance().AssingProjectOfficer(projectId, officerId);
    }

    public Project[] GetIncompleteRequests(int userId)
    {
        if(!_userRequests.containsKey(userId))
        {
            Organization userOrg = OrganizationManager.GetInstance().GetUserOrganization(userId);
            Project[] userRequests = ProjectDataManager.GetInstance().GetIncompleteRequests(userOrg, userId);
            if(userRequests != null)
                _userRequests.put(userId, userRequests);
            else return new Project[0];
        }
        return _userRequests.get(userId);
    }

    public Project GetIncompleteRequest(int userId, int requestId)
    {
        if(_userRequests.containsKey(userId))
        {
            Project[] userRequests = _userRequests.get(userId);
            for(int reqIndex = 0; reqIndex < userRequests.length; reqIndex++)
                if(userRequests[reqIndex].getIdProyecto() == requestId)
                    return userRequests[reqIndex];
            return null;
        }
        return null;
    }

    private void loadUserRequests()
    {
    }

    public boolean CreateRequest(String name, Date iniDate, Date endDate, String bankAcc, double iniBudget,
            int countryId, String description, String objectives, int orgId, int userId)
    {
        boolean result = ProjectDataManager.GetInstance().CreateRequest(name, endDate, iniDate, endDate,
                bankAcc, iniBudget, CommonDataManager.GetInstance().GetCountry(countryId),
                description, objectives, OrganizationManager.GetInstance().GetOrganization(orgId), userId);
        // Hago esto para que se tenga que refrescar los requests del usuario
        if(result)
            _userRequests.remove(userId);
        return result;
    }

    public boolean EditRequest(String name, Date iniDate, Date endDate, String bankAcc, double iniBudget,
            int countryId, String description, String objectives, int orgId, int userId)
    {
        boolean result = ProjectDataManager.GetInstance().CreateRequest(name, endDate, iniDate, endDate,
                bankAcc, iniBudget, CommonDataManager.GetInstance().GetCountry(countryId),
                description, objectives, OrganizationManager.GetInstance().GetOrganization(orgId), userId);
        if(result)
            // Hago esto para que se tenga que refrescar los requests del usuario, la próxima vez que los pida
            _userRequests.remove(userId);
        return result;
    }

    // <editor-fold defaultstate="collapsed" desc="Get Projects">
    public ProjectSummary[] GetProjects(Enums.ProjectState status, Enums.ProjectType type, int pageNumber, int projectsCount)
    {
        return ProjectDataManager.GetInstance().GetProjects(status, type, pageNumber, projectsCount);
    }

    public ProjectSummary[] GetProjectsByType(Enums.ProjectType type, int pageNumber, int projectsCount)
    {
        return ProjectDataManager.GetInstance().GetProjectsByType(type, pageNumber, projectsCount);
    }

    public ProjectSummary[] GetProjectsByCountry(int countryId, Enums.ProjectType type, int pageNumber, int projectsCount)
    {
        return ProjectDataManager.GetInstance().GetProjectsByCountry(countryId, type, pageNumber, projectsCount);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Count Projects">
    public int GetProjectsCount(Enums.ProjectType type, Enums.ProjectState status)
    {
        return ProjectDataManager.GetInstance().GetProjectsCount(type, status);
    }

    public int GetProjectsCount(Enums.ProjectType type)
    {
        return ProjectDataManager.GetInstance().GetProjectsCount(type);
    }

    public int GetProjectsCount(Enums.ProjectType type, int countryId)
    {
        return ProjectDataManager.GetInstance().GetProjectsCount(type, countryId);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Cambio de estado">
    public void ApproveRequest(int requestId)
    {
        ProjectDataManager.GetInstance().ChangeStatus(requestId, Enums.ProjectState.approved);
        ProjectDataManager.GetInstance().CreateProject(requestId);
    }

    public void RejectRequest(int requestId)
    {
        ProjectDataManager.GetInstance().ChangeStatus(requestId, Enums.ProjectState.rejected);
    }
    // </editor-fold>

    // </editor-fold>
}
